package ngames.com.sushitv.api;

import com.alibaba.fastjson.JSON;
import com.google.gson.Gson;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import ngames.com.sushitv.core.ErrorEvent;
import ngames.com.sushitv.entity.ItemVideo;
import ngames.com.sushitv.entity.UserInfo;

/**
 * Created by Administrator on 2016/1/14.
 */
public class JSONResolve {
    public static String KEY_CODE = "code";
    public static String KEY_MSG = "message";
    public static String KEY_DATA = "data";
    public static String CODE_SUCCESS = ErrorEvent.CODE_SUCCESS;
    public static String CODE_ERROR = ErrorEvent.CODE_ERROR;
    public static String CODE_ERROR_RESOLVE = ErrorEvent.CODE_ERROR_RESOLVE;         //解析出错

    private static JSONObject resolveObjCode(ApiResponse apiResponse, String response) {
        JSONObject dataObj = null;
        try {
            JSONObject responseJSON = new JSONObject(response);
            String code = responseJSON.optString(KEY_CODE, CODE_ERROR);
            String msg = responseJSON.optString(KEY_MSG);
            apiResponse.setCode(code);
            apiResponse.setMsg(msg);
            if (CODE_SUCCESS.equals(code)) {
                dataObj = responseJSON.getJSONObject(KEY_DATA);
            }
        } catch (JSONException e) {
            e.printStackTrace();
            apiResponse.setCode(CODE_ERROR_RESOLVE);
        }
        return dataObj;
    }

    /**
     * 返回数组类型数据
     *
     * @param apiResponse
     * @param response
     * @return
     */
    private static JSONArray resolveArrayCode(ApiResponse apiResponse, String response) {
        JSONArray dataArray = null;
        try {
            JSONObject responseJSON = new JSONObject(response);
            String code = responseJSON.optString(KEY_CODE, CODE_ERROR);
            String msg = responseJSON.optString(KEY_MSG);
            apiResponse.setCode(code);
            apiResponse.setMsg(msg);
            if (CODE_SUCCESS.equals(code)) {
                dataArray = responseJSON.getJSONArray(KEY_DATA);
            }
        } catch (JSONException e) {
            e.printStackTrace();
            apiResponse.setCode(CODE_ERROR_RESOLVE);
        }
        return dataArray;
    }

    private static String resolveDataStrCode(ApiResponse apiResponse, String response) {
        String dataStr = null;
        try {
            JSONObject responseJSON = new JSONObject(response);
            String code = responseJSON.optString(KEY_CODE, CODE_ERROR);
            String msg = responseJSON.optString(KEY_MSG);
            apiResponse.setCode(code);
            apiResponse.setMsg(msg);
            if (CODE_SUCCESS.equals(code)) {
                dataStr = responseJSON.getString(KEY_DATA);
            }
        } catch (JSONException e) {
            e.printStackTrace();
            apiResponse.setCode(CODE_ERROR_RESOLVE);
        }
        return dataStr;
    }

    /**
     * 只需要解析code，data为空字符
     *
     * @param response
     * @return
     */
    public static ApiResponse<Void> resolveCode(String response) {
        ApiResponse<Void> apiResponse = new ApiResponse();
        try {
            JSONObject responseJSON = new JSONObject(response);
            apiResponse.setCode(responseJSON.optString(KEY_CODE, CODE_ERROR));
            apiResponse.setMsg(responseJSON.optString(KEY_MSG));
        } catch (JSONException e) {
            e.printStackTrace();
            apiResponse.setCode(CODE_ERROR_RESOLVE);
        }
        return apiResponse;
    }

    /**
     * data只有一个string元素
     *
     * @param response
     * @param dataKey
     * @return
     */
    public static ApiResponse<String> dataString(String response, String dataKey) {
        ApiResponse<String> apiResponse = new ApiResponse();
        JSONObject dataObj = resolveObjCode(apiResponse, response);
        if (dataObj == null) {
            return apiResponse;
        }
        apiResponse.setObj(dataObj.optString(dataKey));
        return apiResponse;
    }

    /**
     * 注册
     *
     * @param response
     * @return
     */
    public static ApiResponse<UserInfo> register(String response) {
        ApiResponse<UserInfo> apiResponse = new ApiResponse();
        JSONObject dataObj = resolveObjCode(apiResponse, response);
        if (dataObj == null) {
            return apiResponse;
        }
        UserInfo userInfo = new UserInfo();
        apiResponse.setObj(userInfo);
        return apiResponse;
    }

    public static <T> ApiResponse<T> resolveGSONData(String response, Class<T> type) {
        ApiResponse<T> apiResponse = new ApiResponse<>();
        String data = resolveDataStrCode(apiResponse, response);
        if (data == null) {
            return apiResponse;
        }
        T t = new Gson().fromJson(data, type);
        apiResponse.setObj(t);
        return apiResponse;
    }
    public static <T> ApiResponse<T> resolveGSONData(String key,String response, Class<T> type) {
        try {
            ApiResponse<T> apiResponse = new ApiResponse<>();
            String data = resolveDataStrCode(apiResponse, response);
            if (data == null) {
                return apiResponse;
            }
            JSONObject js = new JSONObject(data);
            String s = js.opt(key).toString();
            T t = new Gson().fromJson(s, type);
            apiResponse.setObj(t);
            return apiResponse;
        }catch (JSONException e){
            return null;
        }
    }
    public static <T> ApiResponse<ArrayList<T>> resolveGSONListData(String key, String response, Class<T[]> type) {
        try {
            ApiResponse<ArrayList<T>> apiResponse = new ApiResponse<>();
            String data = resolveDataStrCode(apiResponse, response);
            if (data == null) {
                return apiResponse;
            }
//        Type type = new TypeToken<ArrayList<T>>() {}.getType();

            JSONObject js = new JSONObject(data);
            String s=js.opt(key).toString();
            T[] array = new Gson().fromJson(s, type);
            final int length = array == null ? 0 : array.length;
            ArrayList<T> list = null;
            if (length > 0) {
                list = new ArrayList<>(length);
                for (int i = 0; i < length; i++) {
                    list.add(array[i]);
                }
            }
            apiResponse.setObj(list);
            return apiResponse;
        } catch (JSONException e) {
            return null;
        }

    }

    public static <T> ApiResponse<ArrayList<T>> resolveGSONListData(String response, Class<T[]> type) {
//        LogUtil.d("JSONResolve", "resolveGSONListData() " + response);
        ApiResponse<ArrayList<T>> apiResponse = new ApiResponse<>();
        String data = resolveDataStrCode(apiResponse, response);
        if (data == null) {
            return apiResponse;
        }
//        Type type = new TypeToken<ArrayList<T>>() {}.getType();
        T[] array = new Gson().fromJson(data, type);
        final int length = array == null ? 0 : array.length;
        ArrayList<T> list = null;
        if (length > 0) {
            list = new ArrayList<>(length);
            for (int i = 0; i < length; i++) {
                list.add(array[i]);
            }
        }
        apiResponse.setObj(list);
        return apiResponse;
    }
}
